package top.yudoge.dogeeditor.app.web

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import top.yudoge.dogeeditor.app.query.*
import top.yudoge.dogeeditor.commons.web.Response
import top.yudoge.dogeeditor.db.api.DataSourceMetaDataApi
import top.yudoge.dogeeditor.db.objects.*

@RestController
@RequestMapping("/ds-metadata")
class DataSourceMetaDataController {

    @Autowired
    private lateinit var dataSourceMetaDataApi: DataSourceMetaDataApi

    @PostMapping("/data-sources")
    fun getDataSources(): Response<List<DataSource>> {
        return Response.success(dataSourceMetaDataApi.getDataSources())
    }

    @PostMapping("/databases")
    fun getDatabases(@RequestBody query: GetDatabasesQuery): Response<List<Database>> {
        return Response.success(dataSourceMetaDataApi.getDatabases(query.dataSourceIdentifier))
    }

    @PostMapping("/schemas")
    fun getSchemas(@RequestBody query: GetSchemasQuery): Response<List<Schema>> {
        return Response.success(dataSourceMetaDataApi.getSchemas(query.dataSourceIdentifier, query.databaseName))
    }

    @PostMapping("/tables")
    fun getTables(@RequestBody query: GetTablesQuery): Response<List<Table>> {
        return Response.success(dataSourceMetaDataApi.getTables(query.dataSourceIdentifier, query.databaseName, query.schemaName))
    }

    @PostMapping("/columns")
    fun getColumns(@RequestBody query: GetColumnsQuery): Response<List<Column>> {
        return Response.success(dataSourceMetaDataApi.getColumns(query.dataSourceIdentifier, query.databaseName, query.schemaName, query.tableName))
    }

    @PostMapping("/indexes")
    fun getIndexes(@RequestBody query: GetIndexesQuery): Response<List<Index>> {
        return Response.success(dataSourceMetaDataApi.getIndexes(query.dataSourceIdentifier, query.databaseName, query.schemaName, query.tableName))
    }
}